﻿namespace AnyOffice.Components
{
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.Caching;
    using System.Reflection;

    public class ApplicationCache
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public static void Update()
        {
            try
            {
                Hashtable hashtable = new Hashtable(new myCultureComparer());
                string str = "SELECT [FuncId],[FuncCode] FROM [dbo].[Sys_Function]";
                SqlDataReader reader = SqlHelper.ExecuteReader(ConfigManager.ConnectionString, CommandType.Text, str);
                while (reader.Read())
                {
                    hashtable[reader.GetString(1).ToLower()] = reader.GetInt32(0);
                }
                reader.Close();
                hashtable = Hashtable.Synchronized(hashtable);
                HttpContext.Current.Cache.Insert("AnyOffice_SysFunction", hashtable, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                str = "SELECT * FROM [dbo].[MyTable] ORDER BY [ModulePos] ASC,[ModuleNo] ASC";
                DataTable table = SqlHelper.ExecuteDataset(ConfigManager.ConnectionString, CommandType.Text, str).Tables[0];
                table.TableName = "MyTable";
                HttpContext.Current.Cache.Insert("AnyOffice_MyTable", table, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                str = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] ORDER BY [DeptNo] ASC";
                DataTable table2 = SqlHelper.ExecuteDataset(ConfigManager.ConnectionString, CommandType.Text, str).Tables[0];
                table.TableName = "Department";
                HttpContext.Current.Cache.Insert("AnyOffice_Department", table2, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static void UpdateDepartment()
        {
            try
            {
                string str = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] ORDER BY [DeptNo] ASC";
                DataTable table = SqlHelper.ExecuteDataset(ConfigManager.ConnectionString, CommandType.Text, str).Tables[0];
                table.TableName = "Department";
                HttpContext.Current.Cache.Insert("AnyOffice_Department", table, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static void UpdateMyTable()
        {
            try
            {
                string str = "SELECT * FROM [dbo].[MyTable] ORDER BY [ModulePos] ASC,[ModuleNo] ASC";
                DataTable table = SqlHelper.ExecuteDataset(ConfigManager.ConnectionString, CommandType.Text, str).Tables[0];
                table.TableName = "MyTable";
                HttpContext.Current.Cache.Insert("AnyOffice_MyTable", table, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static void UpdateSysFunction()
        {
            try
            {
                Hashtable table = new Hashtable(new myCultureComparer());
                string str = "SELECT [FuncId],[FuncCode] FROM [dbo].[Sys_Function]";
                SqlDataReader reader = SqlHelper.ExecuteReader(ConfigManager.ConnectionString, CommandType.Text, str);
                while (reader.Read())
                {
                    table[reader.GetString(1).ToLower()] = reader.GetInt32(0);
                }
                reader.Close();
                table = Hashtable.Synchronized(table);
                HttpContext.Current.Cache.Insert("AnyOffice_SysFunction", table, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static DataTable Department
        {
            get
            {
                if (HttpContext.Current.Cache["AnyOffice_Department"] == null) Update();
                return (DataTable) HttpContext.Current.Cache["AnyOffice_Department"];
            }
        }

        public static DataTable MyTable
        {
            get
            {
                if (HttpContext.Current.Cache["AnyOffice_MyTable"] == null) Update();
                return (DataTable) HttpContext.Current.Cache["AnyOffice_MyTable"];
            }
        }

        public static Hashtable SysFunction
        {
            get
            {
                if (HttpContext.Current.Cache["AnyOffice_SysFunction"] == null) Update();
                return (Hashtable) HttpContext.Current.Cache["AnyOffice_SysFunction"];
            }
        }
    }
}

